Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(confighttp): add max_redirects configuration option #10877

Open
wants to merge 27 commits into
base: main
Choose a base branch
from

Conversation

rogercoll
Copy link
Contributor

@rogercoll rogercoll commented Aug 13, 2024

Description

Link to tracking issue

#10870

Testing

Unit testing

Documentation

README.md

Copy link

codecov bot commented Aug 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.45%. Comparing base (7f6dc01) to head (76807a3).
Report is 73 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #10877   +/-   ##
=======================================
  Coverage   91.45%   91.45%           
=======================================
  Files         438      438           
  Lines       23827    23837   +10     
=======================================
+ Hits        21791    21801   +10     
  Misses       1658     1658           
  Partials      378      378           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@atoulme
Copy link
Contributor

atoulme commented Aug 13, 2024

Changes look great, if you would please reduce the diff to avoid having git history be impacted too much.
As you point out, also need to add the option to README.

@rogercoll rogercoll force-pushed the add_max_redirects_confighttp branch from 163192c to 26bd593 Compare August 14, 2024 09:39
@rogercoll
Copy link
Contributor Author

@atoulme The lint CI is failing after removing the auto formatting changes:

confighttp_test.go:40: File is not `gofmt`-ed with `-s` (gofmt)
type customRoundTripper struct{

Should we fix them here? Or in a new PR?

@rogercoll
Copy link
Contributor Author

The lint CI is failing after removing the auto formatting changes

My bad, it was because of a removed space in the diffs. Moving the PR ready for review

@rogercoll rogercoll marked this pull request as ready for review August 14, 2024 14:39
@rogercoll rogercoll requested review from a team and atoulme August 14, 2024 14:39
@rogercoll
Copy link
Contributor Author

@atoulme Any update on this PR? Any additional feedback would be really appreciated. Thanks for taking a look.

@rogercoll rogercoll requested a review from a team as a code owner September 19, 2024 13:13
Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rogercoll, just one question

func makeCheckRedirect(max *int) func(*http.Request, []*http.Request) error {
if max == nil {
return nil
} else if *max == 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this case needed here? if max is set to 0, will the check at line 266 return on the first call the same as is this block here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch! Actually, this branch is not needed and resulted in the same output when max_redirects was set to either 0 or 1. The max redirects function is always called when a redirect response is returned, meaning that at least one request has already been made. The variable via []*http.Request on line 265 will always be > 0.

If max_redirects is set to 0, the http client should not follow any redirect response (one request). If max_redirects is 1, only one redirect response should be followed (2 total requests). max_redirects = total_requests - 1

Fixed in c98fe0a

Thanks!

config/confighttp/confighttp.go Outdated Show resolved Hide resolved
@@ -105,6 +105,9 @@ type ClientConfig struct {
HTTP2PingTimeout time.Duration `mapstructure:"http2_ping_timeout"`
// Cookies configures the cookie management of the HTTP client.
Cookies *CookiesConfig `mapstructure:"cookies"`

// Maximum number of redirections to follow, if not defined, the Client uses its default policy, which is to stop after 10 consecutive requests.
MaxRedirects *int `mapstructure:"max_redirects"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can even keep it int with 10 by default. That should be cleaner. The only problem is it'll be a significant breaking change (no redirects) for a component that doesn't use NewDefaultClientConfig, but I don't think we have any of those in contrib. Maybe we can check... All components should use that anyway. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am totally open to adding this, but I am concert about receivers initiating the config without using the default method (max_redirects will be set to 0). Also, the only difference is that maybe we should align with the Go library error message after 10 redirects:

func defaultCheckRedirect(req *Request, via []*Request) error {
	if len(via) >= 10 {
		return errors.New("stopped after 10 redirects")
	}
	return nil
}

There is this ongoing effort to use the NewDefaultClientConfig: open-telemetry/opentelemetry-collector-contrib#35457

Copy link
Member

@dmitryax dmitryax Nov 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Different message isn't a problem I thought. I would like it to be merged without a pointer after all exporters use the default constructor. This is the pattern we use in other configs I believe. Let me know if that's ok with you

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds reasonable, let's keep it pointer free :) e6b62bb

Should we wait until open-telemetry/opentelemetry-collector-contrib#35457 is done?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added the Stale label Oct 24, 2024
@github-actions github-actions bot removed the Stale label Oct 26, 2024
Copy link
Contributor

This PR was marked stale due to lack of activity. It will be closed in 14 days.

@github-actions github-actions bot added Stale and removed Stale labels Nov 17, 2024
djaglowski pushed a commit to open-telemetry/opentelemetry-collector-contrib that referenced this pull request Dec 6, 2024
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

These components are constructing `confighttp.ClientConfig` field by
field and adding the corresponding defaults. Instead of using default
values to construct a new struct, the new struct should already contain
the defaults.

This becomes an issue when adding new fields with defaults in the
`ClientConfig` structure, see:
open-telemetry/opentelemetry-collector#10877

Error in the core collector CI:

```
=== FAIL: . TestLoadConfig/elasticsearch (re-run 1) (0.00s)
    config_test.go:200: Config mismatch (-expected +actual):
          &elasticsearchreceiver.Config{
          	ControllerConfig: {CollectionInterval: s"2m0s", InitialDelay: s"1s"},
          	ClientConfig: confighttp.ClientConfig{
          		... // 15 identical fields
          		HTTP2PingTimeout: s"0s",
          		Cookies:          nil,
        - 		MaxRedirects:     0,
        + 		MaxRedirects:     10,
          	},
          	MetricsBuilderConfig: {Metrics: {ElasticsearchBreakerMemoryEstimated: {Enabled: true}, ElasticsearchBreakerMemoryLimit: {Enabled: true}, ElasticsearchBreakerTripped: {Enabled: true}, ElasticsearchClusterDataNodes: {Enabled: true}, ...}, ResourceAttributes: {ElasticsearchClusterName: {Enabled: true}, ElasticsearchIndexName: {Enabled: true}, ElasticsearchNodeName: {Enabled: true}, ElasticsearchNodeVersion: {Enabled: true}}},
          	Nodes:                {"_local"},
          	... // 4 identical fields
          }
```
          

<!-- Issue number (e.g. #1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Fixes

<!--Describe what testing was performed and which tests were added.-->
#### Testing

<!--Describe the documentation added.-->
#### Documentation

<!--Please delete paragraphs that you did not use before submitting.-->
ZenoCC-Peng pushed a commit to ZenoCC-Peng/opentelemetry-collector-contrib that referenced this pull request Dec 6, 2024
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

These components are constructing `confighttp.ClientConfig` field by
field and adding the corresponding defaults. Instead of using default
values to construct a new struct, the new struct should already contain
the defaults.

This becomes an issue when adding new fields with defaults in the
`ClientConfig` structure, see:
open-telemetry/opentelemetry-collector#10877

Error in the core collector CI:

```
=== FAIL: . TestLoadConfig/elasticsearch (re-run 1) (0.00s)
    config_test.go:200: Config mismatch (-expected +actual):
          &elasticsearchreceiver.Config{
          	ControllerConfig: {CollectionInterval: s"2m0s", InitialDelay: s"1s"},
          	ClientConfig: confighttp.ClientConfig{
          		... // 15 identical fields
          		HTTP2PingTimeout: s"0s",
          		Cookies:          nil,
        - 		MaxRedirects:     0,
        + 		MaxRedirects:     10,
          	},
          	MetricsBuilderConfig: {Metrics: {ElasticsearchBreakerMemoryEstimated: {Enabled: true}, ElasticsearchBreakerMemoryLimit: {Enabled: true}, ElasticsearchBreakerTripped: {Enabled: true}, ElasticsearchClusterDataNodes: {Enabled: true}, ...}, ResourceAttributes: {ElasticsearchClusterName: {Enabled: true}, ElasticsearchIndexName: {Enabled: true}, ElasticsearchNodeName: {Enabled: true}, ElasticsearchNodeVersion: {Enabled: true}}},
          	Nodes:                {"_local"},
          	... // 4 identical fields
          }
```
          

<!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Fixes

<!--Describe what testing was performed and which tests were added.-->
#### Testing

<!--Describe the documentation added.-->
#### Documentation

<!--Please delete paragraphs that you did not use before submitting.-->
Copy link
Member

@jpkrohling jpkrohling left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I'm OK with the code, I believe this is the type of work that should be done after v1.

@rogercoll
Copy link
Contributor Author

While I'm OK with the code, I believe this is the type of work that should be done after v1.

Is there any reference document/label that tracks features we want to add after v1?

sbylica-splunk pushed a commit to sbylica-splunk/opentelemetry-collector-contrib that referenced this pull request Dec 17, 2024
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

These components are constructing `confighttp.ClientConfig` field by
field and adding the corresponding defaults. Instead of using default
values to construct a new struct, the new struct should already contain
the defaults.

This becomes an issue when adding new fields with defaults in the
`ClientConfig` structure, see:
open-telemetry/opentelemetry-collector#10877

Error in the core collector CI:

```
=== FAIL: . TestLoadConfig/elasticsearch (re-run 1) (0.00s)
    config_test.go:200: Config mismatch (-expected +actual):
          &elasticsearchreceiver.Config{
          	ControllerConfig: {CollectionInterval: s"2m0s", InitialDelay: s"1s"},
          	ClientConfig: confighttp.ClientConfig{
          		... // 15 identical fields
          		HTTP2PingTimeout: s"0s",
          		Cookies:          nil,
        - 		MaxRedirects:     0,
        + 		MaxRedirects:     10,
          	},
          	MetricsBuilderConfig: {Metrics: {ElasticsearchBreakerMemoryEstimated: {Enabled: true}, ElasticsearchBreakerMemoryLimit: {Enabled: true}, ElasticsearchBreakerTripped: {Enabled: true}, ElasticsearchClusterDataNodes: {Enabled: true}, ...}, ResourceAttributes: {ElasticsearchClusterName: {Enabled: true}, ElasticsearchIndexName: {Enabled: true}, ElasticsearchNodeName: {Enabled: true}, ElasticsearchNodeVersion: {Enabled: true}}},
          	Nodes:                {"_local"},
          	... // 4 identical fields
          }
```
          

<!-- Issue number (e.g. open-telemetry#1234) or full URL to issue, if applicable. -->
#### Link to tracking issue
Fixes

<!--Describe what testing was performed and which tests were added.-->
#### Testing

<!--Describe the documentation added.-->
#### Documentation

<!--Please delete paragraphs that you did not use before submitting.-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants